package com.example.java3s1.controller;

import com.example.java3s1.dao.MemoryDatabase;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Controller
public class MvcController {
    @Resource
    private MemoryDatabase memoryDatabase;
    @RequestMapping(value = "/first",method = RequestMethod.GET)
    public ModelAndView first(@RequestParam String param){
        System.out.println(param);
        return new ModelAndView(){{
            setViewName("content");
            addObject("attr", param);
            addObject("datas",memoryDatabase.getPersons());
            addObject("lock" ,memoryDatabase.getLock());

        }};
    }
    @RequestMapping(value = "/second",method = RequestMethod.POST)
    public ModelAndView second(@RequestParam Colors radioEnum
                               ,@RequestParam List<Sizes> checkboxEnums
                                ,@RequestParam MultipartFile target) throws IOException {
        System.out.println(radioEnum+""+checkboxEnums);
        Path path = Paths.get(System.getProperty("user.home")).resolve("requestParam.xlsx");
        target.transferTo(path.toFile());
        return new ModelAndView(){{
            setViewName("content");
            addObject("attr",radioEnum+"==>>"+checkboxEnums.toString());
        }};
    }
    public enum Colors{
        red, green, blue
    }
    public enum Sizes{
        m, l, xl
    }
}
